package com.hsh.maoduo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.maoduo.exception.MyException;
import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.constant.common.DateConstants;
import com.hsh.maoduo.mapper.GoodsMapper;
import com.hsh.maoduo.mapper.InboundMapper;
import com.hsh.maoduo.pojo.dto.InboundDTO;
import com.hsh.maoduo.pojo.entity.Goods;
import com.hsh.maoduo.pojo.entity.Inbound;
import com.hsh.maoduo.pojo.vo.InboundVO;
import com.hsh.maoduo.service.GoodsService;
import com.hsh.maoduo.service.InboundService;
import com.hsh.maoduo.util.DataUtil;
import com.hsh.maoduo.util.MyBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author USER
 * @description 针对表【inbound】的数据库操作Service实现
 * @createDate 2023-12-23 16:35:07
 */
@Service
public class InboundServiceImpl extends ServiceImpl<InboundMapper, Inbound> implements InboundService {

    @Resource
    private InboundMapper inboundMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private GoodsService goodsService;

    @Override
    public List<InboundDTO> getList(InboundVO inboundVO) {
        inboundVO.setGoodsName(DataUtil.removeSpaceAndSymbols(inboundVO.getGoodsName()))
                .setSupplierName(DataUtil.removeSpaceAndSymbols(inboundVO.getSupplierName()))
                .setNo(DataUtil.removeSpaceAndSymbols(inboundVO.getNo()));
        List<InboundDTO> inboundDTOList = inboundMapper.selectListByQuery(inboundVO);
        inboundDTOList.stream().forEach(InboundDTO::init);
        return inboundDTOList;
    }

    @Override
    public Result addList(List<InboundVO> inboundVOList) {
        int insertNum = 0;
        Date now = new Date();
        for (InboundVO inboundVO : inboundVOList) {
            inboundVO.setCreateTime(now);
            inboundVO.setNo(DateConstants.PURE_DATETIME_MS_FORMAT.format(now));
            try {
                insertNum += addOne(inboundVO);
            } catch (MyException e) {
                log.error("添加失败,id为：" + inboundVO.getId());
            }
        }
        return Result.create(insertNum);
    }

    @Override
    @Transactional
    public int addOne(InboundVO inboundVO) {
        Integer goodsId = inboundVO.getGoodsId();
        //获取当前商品信息
        Goods oldGoods = goodsMapper.selectById(goodsId);
        if (oldGoods == null) {
            return 0;
        }
        //修改商品库存
        goodsService.updateGoodsNum(goodsId, inboundVO.getNum());
        //没有设置价格时 获取当前的出库价格
        inboundVO.setPriceCent(oldGoods.getInPriceCent());
        return inboundMapper.insert(MyBeanUtils.copy(inboundVO, Inbound.class));
    }

    /**
     * 只允许修改入库数量
     *
     * @param inboundVO
     * @return
     */
    @Override
    @Transactional
    public Result update(InboundVO inboundVO) {
        Inbound oldInbound = inboundMapper.selectById(inboundVO.getId());
        goodsService.updateGoodsNum(inboundVO.getGoodsId()
                , inboundVO.getNum() - oldInbound.getNum());
        int updateNum = inboundMapper.updateById(MyBeanUtils.copy(inboundVO, Inbound.class));
        return Result.create(updateNum);
    }


    @Override
    @Transactional
    public Result delete(int id) {
        Inbound inbound = inboundMapper.selectById(id);
        goodsService.updateGoodsNum(inbound.getGoodsId(), inbound.getNum() * -1);
        int deleteNum = inboundMapper.updateById(inbound.setIsDelete(1));
        return Result.create(deleteNum);
    }

}




